package io.renren.modules.file.controller;

import io.renren.common.utils.Pages;
import io.renren.common.utils.ResponseModel;
import io.renren.modules.file.entity.FileEntity;
import io.renren.modules.file.service.FileService;
import io.renren.modules.file.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("sys/file")
@Api(tags = "sys-file")

public class SysFileController {
    @Autowired
    private FileService fileService;

    @Autowired
    private FileUploadService fileUploadService;


    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("查询文件列表，默认查询 \"安全管控清单\", \"规范\", \"超前地质预报\", \"课件\", \"法律法规\"")
    public ResponseModel<Pages<FileEntity>> list(@RequestBody FileEntity file) {
        Pages<FileEntity> page = fileService.queryPage(file);
        return ResponseModel.of(page);
    }

    @PostMapping("/listAll")
    @ApiOperation("根据条件查询列表")
    public ResponseModel<List<FileEntity>> listAll(@RequestBody FileEntity fileEntity) {
        List<FileEntity> list = fileService.listAll(fileEntity);
        return ResponseModel.of(list);
    }

    @PostMapping("/uploadMultipleFiles")
    @ApiOperation("批量上传文件返回数量")
    public ResponseModel<Integer> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files, @RequestParam("xtlx") String xtlx) {
        if (files != null && files.length > 0) {
            for (int i = 0, len = files.length; i < len; i++) {
                fileUploadService.storeFile(files[i], xtlx);
            }
        }
        return ResponseModel.of(files.length);
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("删除或批量删除")
    public ResponseModel delete(@PathVariable("id") Long id) {

        FileEntity entity = fileService.getById(id);
        fileUploadService.deletFile(entity);
        fileService.removeById(id);

        return ResponseModel.ok();
    }

}
